package org.devgframwork.datasync.core.support.parser.impl;

import org.devgframwork.datasync.core.support.parser.HandleParser;
import org.devgframwork.datasync.core.utils.SearchUtil;

import java.util.HashMap;
import java.util.Map;

/**
 * @author devg
 * @Date 2019/10/18 10:06
 */
public class SqlTargetParser implements HandleParser {

  private String sql;

  private Map<String, ?> paramMap;

  public SqlTargetParser(String sql, Map<String, ?> paramMap) {
    this.sql = sql;
    this.paramMap = paramMap;
  }

  @Override
  public String getText() {
    if (null == paramMap || paramMap.isEmpty()) {
      paramMap = new HashMap<>();
    }
    for (String key : paramMap.keySet()) {
      String value = SearchUtil.mapToStringNull(paramMap, key);
      sql = SearchUtil.getTargetSqlByParam(sql, key, value);
    }
    return SearchUtil.removeEmptyValue(sql);
  }
}
